<?php
/*
 * @Created by: wangchengcheng
 * @Author: wangchengcheng
 * @Date: 2020-01-10 18:55:13
 * @LastEditors  : wangchengcheng
 * @LastEditTime : 2020-01-10 19:07:29
 */

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use App\Models\Comment as CommentModel;

class Comment implements Rule
{
    private $message;
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        //过滤无意义评论
        if(in_array($value, ['test', '测试'])){
            $this->message = '禁止无意义评论';
            
            return false;
        }

        $commentModel = new CommentModel();
        if(empty($commentModel->imageToUbb($value))){
            $this->message = '内容不能为空';

            return false;
        }

        //获取用户id
        $userId = auth()->guard('socialite')->user()->id;
        //是否是管理员
        $isAdmin = auth()->guard('socialite')->user()->is_admin;
        //获取当前时间戳
        $time = time();
        //获取最后一次评论时间
        $lastCommentDate = $commentModel->where('socialite_user_id', $userId)
            ->orderBy('created_at', 'desc')
            ->value('created_at');
        $lastCommentTime = strtotime($lastCommentDate);
        //限制1分钟内只评论1次
        if($isAdmin != 1 && $time - $lastCommentTime < 60){
            $this->message = '评论太过频繁,请稍后再试';

            return false;
        }
        //限制用户每天最多评论10条
        $date = date('Y-m-d', $time);
        $count = $commentModel
            ->where('socialite_user_id', auth()->guard('socialite')->user()->id)
            ->whereBetween('created_at', [$date . '00:00:00', $date . '23:59:59'])
            ->count();
        if($isAdmin != 1 && $count > 10){
            $this->message = '评论已被限制.';

            return false;
        }

        return true;

    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return $this->message;
    }
}
